import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:smart_devices/model/device.dart';

class DeviceListLogic extends GetxController {
  RxInt total = RxInt(0);

  RxString search = RxString('');

  late RefreshController refreshController;

  RxList<DeviceModel> pageList = RxList([]);

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 15) {
      return refreshController.loadNoData();
    }

    /// 添加
    refreshController.loadComplete();
    pageList.add(DeviceModel(
        imgUrl:
            'https://img0.baidu.com/it/u=3043235289,336851805&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500',
        name: '电梯设备 ${pageList.length + 1}',
        code: 'TB49897${pageList.length + 1}',
        location: '四厂区二楼三车间'));
  }

  @override
  void onInit() {
    super.onInit();
    for (var i = 0; i < 10; i++) {
      pageList.add(DeviceModel(
          imgUrl:
              'https://img0.baidu.com/it/u=3043235289,336851805&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500',
          name: '电梯设备 ${i + 1}',
          code: 'TB49897$i',
          location: '四厂区二楼三车间'));
    }
    refreshController = RefreshController(initialRefresh: false);
    total.value = Get.arguments ?? 0;
  }

  @override
  void onClose() {
    super.onClose();
    refreshController.dispose();
  }
}
